When a program encounters a While statement, it tests the condition. If the condition is False, the program continues directly following the Wend statement. If the condition is True, the loop is executed until the program finds Wend and then jumps back to the While statement. If the condition is still True, the loop is executed again.
プログラムの実行が While ステートメントに差し掛かると、ループ条件の判定が行われます。この判定結果が False であれば、Wend ステートメント以降のプログラムコードが実行されます。判定結果が True であれば、Wend まで続くループ部のプログラムコードを実行してから、再度 While ステートメントの位置に実行行が戻ります。こうした判定結果が True であり続ける限り、ループ部が繰り返し実行されます。
Unlike the Do...Loop statement, you cannot cancel a While...Wend loop with Exit. Never exit a While...Wend loop with GoTo, since this can cause a run-time error.
Do...Loop とは異なり、While...Wend は Exit でループを中断することができません。また While...Wend ループを GoTo で抜け出すと、実行時エラーが発生することがあるため、この方法でループを中断させることはできません。
A Do...Loop is more flexible than a While...Wend.
こうした理由から、より柔軟な対応の行える Do...Loop の使用が推奨されます。
While Condition [Statement] Wend
While Condition [Statement] Wend
Sub ExampleWhileWend
Sub ExampleWhileWend
Dim stext As String
Dim stext As String
Dim iRun As Integer
Dim iRun As Integer
sText ="This is a short text"
sText ="This is a short text"
iRun = 1
iRun = 1
while iRun < Len(sText)
while iRun < Len(sText)
if Mid(sText,iRun,1 )<> " " then Mid( sText ,iRun, 1, Chr( 1 + Asc( Mid(sText,iRun,1 )) )
if Mid(sText,iRun,1 )<> " " then Mid( sText ,iRun, 1, Chr( 1 + Asc( Mid(sText,iRun,1 )) )
iRun = iRun + 1
iRun = iRun + 1
Wend
Wend
MsgBox sText,0,"Text encoded"
MsgBox sText,0,"Text encoded"
end sub
end sub